package com.teaRestaurant.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.teaRestaurant.domain.DTO.DishPageQueryDTO;
import com.teaRestaurant.domain.VO.DishPageQueryVO;
import com.teaRestaurant.domain.entity.Category;
import com.teaRestaurant.domain.entity.Dishes;
import com.teaRestaurant.mapper.DishMapper;
import com.teaRestaurant.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Override
    public PageInfo<DishPageQueryVO> list(DishPageQueryDTO dishPageQueryDTO) {

//        System.out.println(dishPageQueryDTO);

//        IPage<Dishes> page = new Page<>(pageNum,pageSize);
//        LambdaQueryWrapper<Dishes> wrapper = new LambdaQueryWrapper<>();
//        wrapper.like(Dishes::getName,dishPageQueryDTO.getName());
//        if (dishPageQueryDTO.getCId() != null)
//        wrapper.eq(Dishes::getCId, dishPageQueryDTO.getCId());
//        if (dishPageQueryDTO.getStatus() != null)
//        wrapper.eq(Dishes::getStatus, dishPageQueryDTO.getStatus());
//        IPage<Dishes> categoryIPage = dishMapper.selectPage(page, wrapper);
//        IPage<Dishes> categoryIPage2 = new Page<>();
//        BeanUtils.copyProperties(categoryIPage,categoryIPage2);

        int pageNum = dishPageQueryDTO.getPageNum();
        int pageSize = dishPageQueryDTO.getPageSize();
        System.out.println(pageNum + " " + pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<DishPageQueryVO> dishList = dishMapper.list(dishPageQueryDTO);
        for (DishPageQueryVO dishPageQueryVO : dishList) {
            System.out.println(dishPageQueryVO);
        }
        PageInfo<DishPageQueryVO> pageInfo = new PageInfo<>(dishList);
        return pageInfo;
    }

    @Override
    public void insert(Dishes dishes) {
        System.out.println(dishes.getCId());
        dishes.setOperateTime(LocalDateTime.now());
        dishMapper.insert(dishes);
    }

    @Override
    public void delete(List<Long> ids) {
        for (Long id : ids) {
            dishMapper.deleteById(id);
        }
    }

    @Override
    public void update(Dishes dishes) {
        LambdaQueryWrapper<Dishes> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dishes::getId,dishes.getId());
        dishes.setOperateTime(LocalDateTime.now());
        dishMapper.update(dishes,wrapper);
    }

    @Override
    public void updateStatus(Long id) {
        Dishes dishes = dishMapper.selectById(id);
        dishes.setStatus(dishes.getStatus() == 1?2:1);
        dishes.setOperateTime(LocalDateTime.now());
        LambdaQueryWrapper<Dishes> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dishes::getId,dishes.getId());
        dishMapper.update(dishes,wrapper);
    }

    @Override
    public DishPageQueryVO getById(Long id) {
        DishPageQueryDTO dishPageQueryDTO = DishPageQueryDTO.builder().id(id).build();
        List<DishPageQueryVO> dishList = dishMapper.list(dishPageQueryDTO);
        DishPageQueryVO dishPageQueryVO = dishList.get(0);
        return dishPageQueryVO;
    }

    @Override
    public List<Dishes> listByCId(Long id) {
        LambdaQueryWrapper<Dishes> wrapper = new LambdaQueryWrapper<>();
        if (id != 0 && id != null){
            wrapper.eq(Dishes::getCId,id);
        }
        wrapper.eq(Dishes::getStatus,1);
        List<Dishes> dishesList = dishMapper.selectList(wrapper);
        return dishesList;
    }

    @Override
    public Map getDishSellCount() {
        LambdaQueryWrapper<Dishes> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dishes::getStatus,1);
        Long isSellCount = dishMapper.selectCount(wrapper);

        wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dishes::getStatus,2);
        Long noSellCount = dishMapper.selectCount(wrapper);
        Map map = new HashMap<>();
        map.put("isSellCount",isSellCount);
        map.put("noSellCount",noSellCount);
        return map;
    }

}
